<?php

namespace app\api\controller;

use app\api\validation\Hall;
use app\api\validation\Surname;
use app\payment\PayFactory;
use app\services\PaymentService;
use app\services\PropertyService;
use app\services\WorshipService;
use plugin\admin\app\model\UserPropertyLog;
use plugin\admin\app\model\WorshipAncestor;
use plugin\admin\app\model\WorshipCategory;
use support\Db;
use support\Request;

class WorshipController
{
    public function category(Request $request)
    {
        try {
            $categories = WorshipService::categories();

            return ok_rsp('成功', ['categories' => $categories]);
        } catch (\Exception $e) {
            return err_rsp($e->getMessage());
        }
    }

    public function ancestors(Request $request)
    {
        try {
            $page = $request->post('page', 1);
            $name = $request->post('name', '');
            $categoryId = $request->post('category_id');

            $data = WorshipService::ancestors($page, 10, $categoryId, $name);

            return ok_rsp('成功', $data);
        } catch (\Exception $e) {
            return err_rsp($e->getMessage());
        }
    }

    public function ancestorDetail(Request $request)
    {
        try {
            $id = $request->post('id');

            $detail = WorshipService::ancestorDetail($id);

            return ok_rsp('成功', $detail);
        } catch (\Exception $e) {
            return err_rsp($e->getMessage());
        }
    }

    public function surnameCategories(Request $request)
    {
        try {
            $page = $request->post('page', 1);
            $length = $request->post('length', 10);
            $name = $request->post('name', '');

            $data = WorshipService::surnameCategories($page, $length, $name);

            return ok_rsp('成功', $data);
        } catch (\Exception $e) {
            return err_rsp($e->getMessage());
        }
    }

    public function surnames(Request $request)
    {
        try {
            $page = $request->post('page', 1);
            $categoryId = $request->post('category_id');

            $data = WorshipService::surnames($page, 10, $categoryId);

            return ok_rsp('成功', $data);
        } catch (\Exception $e) {
            return err_rsp($e->getMessage());
        }
    }

    public function topSurname(Request $request)
    {
        try {
            $categoryId = $request->post('category_id');

            $detail = WorshipService::getTopSurname($categoryId);

            return ok_rsp('成功', $detail);
        } catch (\Exception $e) {
            return err_rsp($e->getMessage());
        }
    }

    public function toTopSurname(Request $request)
    {
        try {
            $categoryId = $request->post('category_id');
            $surnameId = $request->post('surname_id');

            WorshipService::topSurname($categoryId, $surnameId);

            return ok_rsp('成功');
        } catch (\Exception $e) {
            return err_rsp($e->getMessage());
        }
    }

    public function toTopHall(Request $request)
    {
        try {
            $categoryId = $request->post('category_id');
            $hallId = $request->post('hall_id');

            WorshipService::topHall($categoryId, $hallId);

            return ok_rsp('成功');
        } catch (\Exception $e) {
            return err_rsp($e->getMessage());
        }
    }

    public function surnameDetail(Request $request)
    {
        try {
            $id = $request->post('id');

            $detail = WorshipService::surnameDetail($id);

            return ok_rsp('成功', $detail);
        } catch (\Exception $e) {
            return err_rsp($e->getMessage());
        }
    }

    public function addSurname(Request $request)
    {
        Db::beginTransaction();
        try {
            $data = $request->post();

            $validate = new Surname();
            if (!$validate->check($data)) {
                throw new \Exception($validate->getError());
            }

            WorshipService::addSurname($data);

            Db::commit();
            return ok_rsp('成功');
        } catch (\Exception $e) {
            Db::rollBack();
            return err_rsp($e->getMessage());
        }
    }

    public function buyGD(Request $request)
    {
        try {
            $payWay = $request->post('pay_way', 'kindness');

            if ($payWay != PayFactory::WECHAT) {
                PaymentService::checkPayPassword($request->post('pay_password'));
            }

            $count = $request->post('count');

            if ($count < 1) {
                throw new \Exception('数量不正确');
            }

            $data = WorshipService::transferToGD($count, $payWay);

            return ok_rsp('成功', $data);
        } catch (\Exception $e) {
            return err_rsp($e->getMessage());
        }
    }

    public function checkAddHall(Request $request)
    {
        $categoryId = $request->post('category_id');

        try {
            WorshipService::checkIsMatchCondition($categoryId);

            return ok_rsp('成功', ['is_add_surname' => true]);
        } catch (\Exception $e) {
            return ok_rsp('成功', ['is_add_surname' => false]);
        }
    }

    public function addHall(Request $request)
    {
        Db::beginTransaction();
        try {
            $data = $request->post();

            $validate = new Hall();

            if (!$validate->check($data)) {
                throw new \Exception($validate->getError());
            }

            WorshipService::addHall($data);

            Db::commit();
            return ok_rsp('成功');
        } catch (\Exception $e) {
            Db::rollBack();
            return err_rsp($e->getMessage());
        }
    }

    public function halls(Request $request)
    {
        try {
            $page = $request->post('page', 1);
            $categoryId = $request->post('category_id');

            $data = WorshipService::halls($page, 10, $categoryId);

            return ok_rsp('成功', $data);
        } catch (\Exception $e) {
            return err_rsp($e->getMessage());
        }
    }

    public function topHall(Request $request)
    {
        try {
            $categoryId = $request->post('category_id');

            $detail = WorshipService::getTopHall($categoryId);

            return ok_rsp('成功', $detail);
        } catch (\Exception $e) {
            return err_rsp($e->getMessage());
        }
    }

    public function hallDetail(Request $request)
    {
        try {
            $id = $request->post('id');

            if (empty($id)) {
                throw new \Exception('ID不能为空');
            }

            $detail = WorshipService::hallDetail($id);

            return ok_rsp('成功', $detail);
        } catch (\Exception $e) {
            return err_rsp($e->getMessage());
        }
    }

    public function updateHall(Request $request)
    {
        try {
            $data = $request->post();

            $validate = new Hall();

            if (!$validate->check($data)) {
                throw new \Exception($validate->getError());
            }

            if (empty($data['id'])) {
                throw new \Exception('ID不能为空');
            }

            WorshipService::updateHall($data);

            return ok_rsp('成功');
        } catch (\Exception $e) {
            return err_rsp($e->getMessage());
        }
    }

    public function products(Request $request)
    {
        try {
            $type = $request->post('type');

            if (empty($type)) {
                throw new \Exception('类型不能为空');
            }

            $detail = WorshipService::products($type);

            return ok_rsp('成功', $detail);
        } catch (\Exception $e) {
            return err_rsp($e->getMessage());
        }
    }

    public function worshipAncestor(Request $request)
    {
        try {

            $id = $request->post('id');
            $productId = $request->post('product_id');
            $count = $request->post('count');

            if ($productId != 0) {
                PaymentService::checkPayPassword($request->post('pay_password'));
            }

            if (empty($id)) {
                throw new \Exception('ID不能为空');
            }

            if ($count < 1) {
                throw new \Exception('不正确的数量');
            }

            WorshipService::worship(1, $id, $productId, $count);

            return ok_rsp('成功');
        } catch (\Exception $e) {
            return err_rsp($e->getMessage());
        }
    }

    public function worshipAncestorList(Request $request)
    {
        try {
            $id = $request->post('id');
            $page = $request->post('page', 1);

            if (empty($id)) {
                throw new \Exception('ID不能为空');
            }

            $list = WorshipService::worshipList($page, 10, 1, $id);

            return ok_rsp('成功', $list);
        } catch (\Exception $e) {
            return err_rsp($e->getMessage());
        }
    }

    public function worshipSurname(Request $request)
    {
        try {

            $id = $request->post('id');
            $productId = $request->post('product_id');
            $count = $request->post('count');

            if ($productId != 0) {
                PaymentService::checkPayPassword($request->post('pay_password'));
            }

            if (empty($id)) {
                throw new \Exception('ID不能为空');
            }

            if ($count < 1) {
                throw new \Exception('不正确的数量');
            }

            WorshipService::worship(2, $id, $productId, $count);

            return ok_rsp('成功');
        } catch (\Exception $e) {
            return err_rsp($e->getMessage());
        }
    }

    public function worshipSurnameList(Request $request)
    {
        try {
            $id = $request->post('id');
            $page = $request->post('page', 1);

            if (empty($id)) {
                throw new \Exception('ID不能为空');
            }

            $list = WorshipService::worshipList($page, 10, 2, $id);

            return ok_rsp('成功', $list);
        } catch (\Exception $e) {
            return err_rsp($e->getMessage());
        }
    }

}